﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using UtilZ.Dotnet.Ex.Model;

namespace UtilZ.Dotnet.Ex.DebugViews
{
#pragma warning disable 8500
    unsafe internal sealed class StructSequenceDebugView<T> where T : struct
    {
        private readonly T* _ptr;
        private readonly long _length;

        public StructSequenceDebugView(SpanZ<T> span)
        {
            this._ptr = span.DataPtr;
            this._length = span.LongLength;
        }

        public StructSequenceDebugView(LocalSpanZ<T> span)
        {
            this._ptr = span.DataPtr;
            this._length = span.LongLength;
        }

        public StructSequenceDebugView(UnmanagedMemoryPointer<T> pointer)
        {
            this._ptr = pointer.DataPtr;
            this._length = pointer.LongLength;
        }

        public StructSequenceDebugView(ValueTypeFlexibleArray<T> arr)
        {
            SpanZ<T> data = arr.GetSpanZ();
            this._ptr = data.DataPtr;
            this._length = data.LongLength;
        }



        [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
        public T[] Items
        {
            get
            {
                T[] array = new T[this._length];
                long byteCount = this._length * sizeof(T);
                fixed (T* ptr = array)
                {
                    NativeMemory.Copy(this._ptr, ptr, new nuint((ulong)byteCount));
                }
                return array;
            }
        }

    }

}
